var mongoose = require('mongoose')

var CartSchema = new mongoose.Schema({
  
  uId: { type: String },
	cId: { type: String },
	cName: { type: String },
	cPrice: { type: String },
	cImgSrc: { type:String } ,
	cQuantity: { type: Number },
	cStatus : { type: Boolean, default: false  },
	meta: {
		createAt: {
		  type: Date,
		  default: Date.now()
		}
	}
})

CartSchema.pre('save', function(next) {
  var Cart = this

  if (this.isNew) {
    this.meta.createAt = this.meta.updateAt = Date.now()
  }
  else {
    this.meta.updateAt = Date.now()
  }

  next()
})

CartSchema.statics = {
  fetch: function(cb) {
    return this
      .find({})
      .sort('meta.updateAt')
      .exec(cb)
  },
  findById: function(id, cb) {
    return this
      .findOne({_id: id})
      .exec(cb)
  }
}

module.exports = CartSchema
